package com.fusion.core.comm.filetransfers;

import static com.fusion.core.CoreSubsystem.BLOCK_SIZE;

import com.fusion.core.CoreSubsystem;
import com.fusion.core.file.hash.Hash;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author SwethaShivakumar 
 * Project: fusion
 * Date: Mar 6, 2009
 * URL: http://fusionwiki.co.cc
 * Mail Me At: shiva@fusionproject.co.cc
 */
public class CompleteFileBlockProvider extends BlockProvider {
    protected FileChannel fileChannel;

    public CompleteFileBlockProvider(int blockNumber, Hash root, CoreSubsystem core) throws IOException {
        super(blockNumber, root, core);

        File f = new File(fd.getFullPath());
        if (!f.exists()) throw new FileNotFoundException("Could not find: "+fd+" in complete files!");

        FileInputStream in = new FileInputStream(f);
        fileChannel = in.getChannel();
        fileChannel.position(((long)blockNumber)*BLOCK_SIZE);

        if(T.t)T.ass(fileChannel.isOpen(), "FileChannel closed!");
        if(T.t)T.trace("Ready to start sending block");
    }

    public int fill(ByteBuffer buf) throws IOException {
        if (!fileChannel.isOpen()) return -1;

        int bs = prepare(buf);

        int n = fileChannel.read(buf);
        read += n;

        if (n == -1 || read >= bs) {
            if(T.t)T.ass(read == bs, "read more then needed!");
            if(T.t)T.info("Block successfulyl provided. Rest is in buffer. Closing file channel.");
            fileChannel.close();
        }
        return n;
    }

    public void updateRead(int read) throws IOException {
        this.read = read;
        fileChannel.position(((long)blockNumber)*BLOCK_SIZE+read);
    }
}
